package com.tencent.qcloud.tuikit.tuichat.order;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cqyc.network.model.OrderInfo;
import com.tencent.qcloud.tuikit.tuichat.R;

import java.util.ArrayList;
import java.util.List;

public class AdapterOrderPopup extends RecyclerView.Adapter<AdapterOrderPopup.ViewHolder> {

    private final Context context;
    private final List<OrderInfo.ListBean> list = new ArrayList<>();
    private OrderListPopup.OrderClickListener orderClickListener;

    public void setOrderClickListener(OrderListPopup.OrderClickListener orderClickListener) {
        this.orderClickListener = orderClickListener;
    }

    public AdapterOrderPopup(Context context) {
        this.context = context;
    }

    public AdapterOrderPopup(Context context, List<OrderInfo.ListBean> list) {
        this.context = context;
        this.list.addAll(list);
    }

    public void setData(List<OrderInfo.ListBean> list) {
        this.list.clear();
        if (list != null && list.size() > 0) {
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addData(List<OrderInfo.ListBean> list) {
        int start = this.list.size();
        this.list.addAll(list);
        notifyItemRangeInserted(start, this.list.size());
    }

    @NonNull
    @Override
    public AdapterOrderPopup.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterOrderPopup.ViewHolder holder, int position) {
        holder.setData(list.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (orderClickListener != null) {
                    orderClickListener.onClick(holder.getLayoutPosition(), list.get(holder.getLayoutPosition()));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(OrderInfo.ListBean data) {
            TextView orderNumber = itemView.findViewById(R.id.order_number);
            TextView time = itemView.findViewById(R.id.time);
            ImageView image = itemView.findViewById(R.id.goods_image);
            TextView name = itemView.findViewById(R.id.goods_name);
            TextView price = itemView.findViewById(R.id.goods_price);
            TextView status = itemView.findViewById(R.id.status_text);

            orderNumber.setText("订单号：" + data.getTrade_sn());
            time.setText(data.getCreated_at());
            Glide.with(image).load(data.getTrade_items().get(0).getSku_img()).into(image);
            name.setText(data.getTrade_items().get(0).getSku_name());
            price.setText(data.getPayment());
            status.setText(data.getTrade_status_desc());
        }
    }
}
